using NUnit.Framework;
using System;
using System.IO;
using MonsterNet;

namespace MonsterNetTests
{
	[TestFixture()]
	public class TestMNSynchronisedFile
	{
		private SetupTearDownDirectories std = new SetupTearDownDirectories();
		
		#region create/delete test file
		[TestFixtureSetUp]
		public void SetupDirectory() {
			std.SetupDirectory();
		}
		
		[TestFixtureTearDown]
		public void DeleteDirectory() {
			std.TearDownDirectory();			
		}
			
		#endregion
		
		[Test()]
		public void TestCase ()
		{
		
			DateTime scanTime = DateTime.Now;
			MNFSObjectLocation location = MNFSObjectLocation.Source;
			Console.WriteLine(std.Pathname);
			
			Console.WriteLine(SetupTearDownDirectories.kTestRootFileName);
			string pathname = Path.Combine(std.Pathname, SetupTearDownDirectories.kTestRootFileName);
			
			Console.WriteLine(pathname);
			
			FileInfo file = new FileInfo(pathname);
			MNSynchronisedFile syncFile = new MNSynchronisedFile(file, std.Pathname, location, scanTime, false);
		
		
			Assert.AreEqual(syncFile.Name, file.Name, @"Filename test");
			Assert.AreEqual(syncFile.Extension, file.Extension, @"Extension test");
			Assert.AreEqual(syncFile.GetDateFoundForLocation(location), scanTime, @"dateFound test");
			Assert.AreEqual(syncFile.GetDateModifiedForLocation(location), file.LastWriteTime, @"DateModified test");
			Assert.AreEqual(syncFile.GetSizeInBytesForLocation(location), file.Length, @"size in bytes test");
			
			//serialise				
			string _serialisedFilePathname = System.IO.Path.Combine(std.Pathname, @"TestMNSynchronisedFileSerialisation.MNSynchronisedFile");
			Console.WriteLine(string.Format(@"serialising file to {0}", _serialisedFilePathname));				
			MNSerialise.SaveToFile<MNSynchronisedFile>(_serialisedFilePathname, syncFile);
			//test for serialised file
			Assert.IsTrue(System.IO.File.Exists(_serialisedFilePathname), @"serialised out file test");
			//deserialise
			System.Threading.Thread.Sleep(500);
			
			MNSynchronisedFile deserialisedFile = MNSerialise.LoadFromFile<MNSynchronisedFile>(_serialisedFilePathname);
			//test the deserialised file
			Assert.IsNotNull(deserialisedFile, @"deserialised file is not null test");
		
			//its base class MNSynchronisedFSObject				
			Assert.AreEqual(syncFile.Name, 
			                deserialisedFile.Name, @"deserialised file Filename test");
			
			Assert.AreEqual(syncFile.GetDateFoundForLocation(location), 
			                deserialisedFile.GetDateFoundForLocation(location), @"deserialised file dateFound test");
			Assert.AreEqual(syncFile.GetDateModifiedForLocation(location), 
			                deserialisedFile.GetDateModifiedForLocation(location), @"deserialised file DateModified test");
		
			
			//first the MNSynchroniseFile values
			Assert.AreEqual(syncFile.Extension, 
			                deserialisedFile.Extension, @"deserialised file Extension test");
			Assert.AreEqual(syncFile.RootlessPath, 
			                deserialisedFile.RootlessPath, @"deserialised file Extension test");
			
			Assert.AreEqual(syncFile.GetSizeInBytesForLocation(location), 
			                deserialisedFile.GetSizeInBytesForLocation(location),  @"deserialised file size in bytes test");				
			 
			
		}
	}
}

